-- MsgMgr.lua

require("core.Store")

MsgMgr         = singleton()
local prop     = property(MsgMgr)
prop:accessor("loadingPanelUI", nil)
prop:accessor("tipPanelUI", nil)
prop:accessor("tipPanelControl", nil)
prop:accessor("tipPanelCloseTime", 0)

function MsgMgr:__init()
    g_event_mgr:addListener(self, "onShowLoadUI")
    g_event_mgr:addListener(self, "onCloseLoadUI")
    g_event_mgr:addListener(self, "onRequestSent")
    g_event_mgr:addListener(self, "onRequestReceipt")
    g_event_mgr:addListener(self, "onWarnTip")
    g_event_mgr:addListener(self, "onSuccessTip")
    g_event_mgr:addListener(self, "onProgress")
end

function MsgMgr:onShowLoadUI()
    if not self.loadingPanelUI then
        self.loadingPanelUI = Fairy_Create_GUI("LoadingUI")
    end
    self.loadingPanelUI:show()
end

function MsgMgr:onCloseLoadUI()
    if not self.loadingPanelUI then
        return
    end
    self.loadingPanelUI:hide()
end

function MsgMgr:onRequestSent()
    self:onShowLoadUI()
end

function MsgMgr:onRequestReceipt()
    self:onCloseLoadUI()
end

function MsgMgr:onProgress(max, val)
    if not self.loadingPanelUI then
        return
    end
    self.loadingPanelUI:onProgress(max, val)
end

function MsgMgr:onWarnTip(msg)
    if not self.tipPanelUI then
        self.tipPanelUI = Fairy_Create_GUI("TipInfoUI")
        self.tipPanelControl = self.tipPanelUI:getController("type")
    end
    self.tipPanelControl:SetSelectedPage("warn")
    self.tipPanelUI:getChild("title").text = msg or "操作失败";
    self.tipPanelUI:open()
    self:closeTipPanel()
end

function MsgMgr:onSuccessTip(msg)
    if not self.tipPanelUI then
        self.tipPanelUI = Fairy_Create_GUI("TipInfoUI")
        self.tipPanelControl = self.tipPanelUI:getController("type")
    end
    self.tipPanelControl:SetSelectedPage("success")
    self.tipPanelUI:getChild("title").text = msg or "操作成功";
    self.tipPanelUI:open()
    self:closeTipPanel()
end

function MsgMgr:closeTipPanel()
    self.tipPanelCloseTime = os.time() + 3
end

function MsgMgr:update(time)
    if self.tipPanelUI and self.tipPanelUI:isOpened() and time >= self.tipPanelCloseTime then
        self.tipPanelUI:close()
    end
end

g_msg_mgr = MsgMgr()
